import zlib

# 1.创建压缩对象
compressor = zlib.compressobj()

file = "1_zlib_压缩数据.py"
compress_chunks = []
with open(file, "rb") as f:
    while True:
        chunk = f.readline()
        if not chunk:
            break
        compress_chunks.append(compressor.compress(chunk))
compress_chunks.append(compressor.flush())

# 合并压缩数据
compress_data = b"".join(compress_chunks)

# 2.穿件解压对象
decompressor = zlib.decompressobj()
decompress_data = decompressor.decompress(compress_data)
decompress_data += decompressor.flush()

with open("decompress_data", "wb") as f:
    f.write(decompress_data)